Public Class ernClient
    'PUBLIC
    Public clients() As ernClients

    'PRIVATE
    Dim count As Integer = -1
    Dim ernClientsLock As New Object
    Private clientHashes() As String

    'FUNCTIONS
    Public Sub newClient(ByRef newConnection As TcpClient, Optional ByVal isAdmin As Boolean = False)
        SyncLock ernClientsLock
            'make room in the clients array
            count = count + 1
            ReDim Preserve clients(count)
            ReDim Preserve clientHashes(count)
            'create a new thread for the client and name it the client id and set everything up
            If isAdmin Then
                clients(count).thread = New System.Threading.Thread(AddressOf handleAdminClient)
            Else
                clients(count).thread = New System.Threading.Thread(AddressOf handleClient)
            End If
            clientHashes(count) = clients(count).thread.GetHashCode
            clients(count).thread.Name = count
            clients(count).connection = newConnection
            clients(count).inConversation = False
            'start new thread to handle client connection
            clients(count).thread.Start()
        End SyncLock
    End Sub

    Public Sub removeClient(ByVal index As Integer)
        SyncLock ernClientsLock
            If count = index Then
                If count = 0 Then
                    'only 1 client - be careful not to redim (-1)
                    count = -1
                    clients = Nothing
                    clientHashes = Nothing
                Else
                    'I'm removing the last one - just redim it
                    count = count - 1
                    ReDim Preserve clients(count)
                    ReDim Preserve clientHashes(count)
                End If
            Else
                'copy the last item to this item and then redim!
                clients(index) = clients(count)
                clientHashes(index) = clientHashes(count)
                'clients(index).thread.Name = index
                count = count - 1
                ReDim Preserve clients(count)
                ReDim Preserve clientHashes(count)

            End If
        End SyncLock
    End Sub

    Public Function getIndexByHash(ByVal hash As Integer) As Integer
        Dim i As Integer
        Dim returner As Integer = -1

        SyncLock ernClientsLock
            If Not clientHashes Is Nothing Then
                For i = 0 To clientHashes.Length - 1
                    If clientHashes(i) = hash Then returner = i
                Next
            End If
        End SyncLock

        Return returner
    End Function

    Public Sub closeAllConnections()
        Dim i As Integer

        For i = 0 To count
            clients(i).connection.Close()
        Next
    End Sub

    Public Sub stopAllThreads()
        Dim i As Integer

        For i = 0 To count
            clients(i).thread.Abort()
        Next
    End Sub
End Class

Public Structure ernClients
    Public guid As String
    Public ip As String
    Public thread As System.Threading.Thread
    Public connection As TcpClient
    Public computerName As String
    Public inConversation As Boolean
    Public clientVersion As String
    Public policy As policyType
End Structure


